<template>
  <div class="app-container">
    <el-card>
      <div class="filter-container">
        <el-button type="primary" icon="el-icon-back" @click="$router.go(-1)">返回</el-button>
      </div>
      
      <el-dialog title="新增签到" :visible.sync="dialogFormVisible" width="600px">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="活动选择" prop="activityId">
            <activity-select v-model="form.activityId" placeholder="请选择活动" />
          </el-form-item>
          <el-form-item label="用户选择" prop="enrollmentId">
            <el-input
              v-model="form.enrollmentId"
              placeholder="请输入报名ID"
              clearable
              @keyup.enter.native="handleSubmit"
            />
          </el-form-item>
          <el-form-item label="签到时间" prop="signTime">
            <el-date-picker
              v-model="form.signTime"
              type="datetime"
              value-format="timestamp"
              placeholder="选择签到时间"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { addSign } from '@/api/secondclass/sign'
import ActivitySelect from '@/components/ActivitySelect'

export default {
  components: { ActivitySelect },
  data() {
    return {
      dialogFormVisible: true,
      form: {
        activityId: '',
        enrollmentId: '',
        signTime: ''
      },
      rules: {
        activityId: [
          { required: true, message: '请选择活动', trigger: 'blur' }
        ],
        enrollmentId: [
          { required: true, message: '请输入报名ID', trigger: 'blur' }
        ],
        signTime: [
          { required: true, message: '请选择签到时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          addSign(this.form).then(() => {
            this.$message.success('新增成功')
            this.dialogFormVisible = false
            this.$router.push('/secondclass/sign')
          })
        }
      })
    }
  }
}
</script>